package com.dengqi.mymodule.activity.glide.activity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.bumptech.glide.load.engine.GlideException;
import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;
import com.dengqi.mymodule.tools.FileUtils;
import com.module.imagelib.glide.ImageLoader;
import com.module.imagelib.glide.progress.CircleProgressView;
import com.module.imagelib.glide.strategy.GlideStrategy;
import com.module.imagelib.glide.ui.ZoomImageView;

/**
 * Created by dell on 2018/6/25
 */
public class GlideDetailActivity extends BaseActivity {

    String url = "https://desk-fd.zol-img.com.cn/t_s2560x1600c5/g5/M00/02/09/ChMkJlbKzgeIIIVKABqyglb_D50AALJGwEopAgAGrKa319.jpg";
//    String url = "https://desk-fd.zol-img.com.cn/t_s2880x1800c5/g5/M00/04/0E/ChMkJlxG_ruIR3nAABa65fI7gMMAAuhmQIXEVwAFrr9023.jpg";
    ZoomImageView zoomImageView;
    CircleProgressView circleProgressView;
    Bitmap result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glide_detail);
        initWindow();

        zoomImageView = findViewById(R.id.glideView);
        circleProgressView = findViewById(R.id.circleProgressView);


        zoomImageView.setBackgroundColor(Color.GRAY);
//        ImageLoader_v1.getInstance().load(this, url).cache().progress(new OnProgressListener() {
//            @Override
//            public void onProgress(String imageUrl, long bytesRead, long totalBytes, final boolean isDone, GlideException exception) {
//                final int percent = (int) ((bytesRead * 1.0f / totalBytes) * 100.0f);
//                circleProgressView.setProgress(percent);
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        circleProgressView.setVisibility(isDone ? View.GONE : View.VISIBLE);
//                    }
//                });
//            }
//        }).into(zoomImageView);


        ImageLoader.getInstance().setImageLoader(new GlideStrategy(this));
        ImageLoader.getInstance()
                .load(url)
                .progress((int percent, boolean isDone, GlideException exception) -> {
                    circleProgressView.setProgress(percent);
                    runOnUiThread(() -> {
                        circleProgressView.setVisibility(isDone ? View.GONE : View.VISIBLE);
                    });
                })
                .skipDiskCache(true)
                .skipMemoryCache(true)
                .into(zoomImageView);

//        ImageLoader.getInstance().load(url).asFile().into(new FileCallBack());
//        ImageLoader.getInstance().load(url).asBitmap().into(new BitmapCallBack());


//        GlideApp.with(this).load(url).into(zoomImageView);

//        GlideApp.with(this).asBitmap().load(url).into(new SimpleTarget<Bitmap>() {
//            @Override
//            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                zoomImageView.setImageBitmap(resource);
//            }
//        });
    }


    public void onBtn1(View view) {
        if (result == null) {
            return;
        }
        Toast.makeText(this, "保存", Toast.LENGTH_SHORT).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                FileUtils.writeBitmap2File(result, System.currentTimeMillis() + ".jpg");
//                ImageFileUtils.write2album(GlideDetailActivity.this, result);
            }
        }).start();
    }


    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0以上的沉浸式
            //让应用主题内容占用系统状态栏的空间,注意:下面两个参数必须一起使用 stable 牢固的
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            //设置状态栏颜色为透明
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4以上的沉浸式
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }


}
